//
//  DebugManagerUtil.swift
//  YKSwiftDebugManager
//
//  Created by linghit on 2021/12/2.
//

import UIKit

internal class DebugManagerUtil: NSObject {
    
    internal static let share:DebugManagerUtil = DebugManagerUtil.init()
    
    private let itemImageStr = "itemImageStr"
    private let itemTitleStr = "itemTitleStr"
    private let ifdissMissMainStr = "ifdissMissMainStr"
    private let itemClickBlockStr = "itemClickBlockStr"
    private let itemAfterClickDissMiss = "itemAfterClickDissMiss"
    private let switchItemStr = "switchItemStr"
    private let switchItemBlock = "switchItemBlock"
    private let switchItemNotifName = "switchItemNotifName"
    
    private lazy var itemArray:Array<Dictionary<String,Any>> = {
        let items:Array<Dictionary<String,Any>> = []
        return items
    }()
    private lazy var switchArray:Array<Dictionary<String,Any>> = {
        let switchArray:Array<Dictionary<String,Any>> = []
        return switchArray
    }()
    private lazy var diyArray:Array<Dictionary<String,Any>> = {
        let diys:Array<Dictionary<String,Any>> = []
        return diys
    }()

    private override init() {
        super.init()
        
        addNormalItem(itemImage: UIImage.init(named: "ic_main_item_appinfo") ?? UIImage.init(), itemTitle: "应用信息") { callBack, dissMissMain in
            let vc = DebugBaseViewController.init()
            callBack(vc,0,true)
        }
        
        addNormalItem(itemImage: UIImage.init(named: "ic_main_item_userdefatult") ?? UIImage.init(), itemTitle: "本地沙盒") { callBack, dissMissMain in
            
            let vc = DebugBaseViewController.init()
            callBack(vc,0,true)
        }
        
        if let lokin = NSClassFromString("LookinObject") {
            addNormalItem(itemImage: UIImage.init(named: "Lookin_Logo") ?? UIImage.init(), itemTitle: "Lookin") { callBack, dissMissMain in
                
                NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "Lookin_3D"), object: nil)
                dissMissMain()
                
            }
        }
        
        
        
    }
    
    public func numberOfSections()->Int
    {
        return 3
    }
    
    public func numberOfItemsInSection(section:Int)->Int
    {
        if section == 0 {
            return switchArray.count
        }else if section == 1 {
            return itemArray.count
        }else if section == 2 {
            return diyArray.count
        }
        return 0
    }
    
    public func cellForItem(indexPath:IndexPath)->String
    {
        var cellName = ""
        if indexPath.section == 0 {
            cellName = "DebugMainSwitchCell"
        }else if indexPath.section == 1{
            cellName = "DebugMainItemCell"
        }else if indexPath.section == 2{
            cellName = "DebugMainItemCell"
        }
        return cellName
    }
    
    public func nameOfSections(indexPath:IndexPath)->String
    {
        var title = ""
        if indexPath.section == 0 {
            title = "功能开关"
        }else if indexPath.section == 1{
            title = "基本功能"
        }else if indexPath.section == 2{
            title = "自定义功能"
        }
        return title
    }
    
    public func addSwitchItemDetail(switchName:String, switchItemNotificationName:String, itemCallBack:@escaping ((Bool)->Bool))->Void{
        var dic:Dictionary<String,Any> = [:]
        dic.updateValue(switchName, forKey: switchItemStr)
        dic.updateValue(itemCallBack, forKey: switchItemBlock)
        dic.updateValue(switchItemNotificationName, forKey: switchItemNotifName)
        self.switchArray.append(dic)
        
    }
    
    public func getSwitchDetail(indexPath:IndexPath, detailCallBack:@escaping((_ itemName:String, _ switchNBName:String, _ callBack:@escaping ((Bool)->Bool))->Void))->Void{
        let dic:Dictionary<String,Any> = self.switchArray[indexPath.row]
        detailCallBack(dic[switchItemStr] as! String,dic[switchItemNotifName] as! String,dic[switchItemBlock] as! (Bool) -> Bool)
    }
    
    
    public func addCustomItem(itemImage:UIImage, itemTitle:String, itemClickBlock:@escaping((_ callBack: ((UIViewController,Int,Bool)->Void),_ dissMissMain:(()->Void))->Void))->Void{
        
        var dic:Dictionary<String,Any> = [:]
        dic.updateValue(itemImage, forKey: itemImageStr)
        dic.updateValue(itemTitle, forKey: itemTitleStr)
        dic.updateValue(itemClickBlock, forKey: itemClickBlockStr)
        self.diyArray.append(dic)
    }
    
    public func getCustomItem(indexPath:IndexPath, detailBlock:@escaping ((_ itemImage:UIImage, _ titleStr:String)->Void))->Void {
        if indexPath.section == 0 {
            return
        }
        var dic:Dictionary<String,Any>? = nil;
        if indexPath.section == 1 {
            dic = self.itemArray[indexPath.row];
        }else if (indexPath.section == 2) {
            dic = self.diyArray[indexPath.row];
        }
         
        guard let dictionary = dic else {
            return
        }
        
        let itemImage = dictionary[itemImageStr] as? UIImage ?? UIImage.init()
        
        let titleS = dictionary[itemTitleStr] as? String ?? ""
        
        detailBlock(itemImage,titleS)
    }
    
    public func itemClick(indexPath:IndexPath, controller:UIViewController)->Void {
        var dic:Dictionary<String,Any>? = nil;
        if indexPath.section == 1 {
            dic = self.itemArray[indexPath.row];
        }else if (indexPath.section == 2) {
            dic = self.diyArray[indexPath.row];
        }
         
        guard let dictionary = dic else {
            return
        }
        
        let block = dictionary[itemClickBlockStr] as! (_ callBack: ((UIViewController,Int,Bool)->Void),_ dissMissMain:(()->Void))->Void
        
        let callBackCon:((UIViewController,Int,Bool)->Void) = {
            con,type,animated in
            if type == 0 {
                
                controller.navigationController?.pushViewController(con, animated: animated)
            }else if type == 1 {
                controller.navigationController?.present(con, animated: animated, completion: nil)
            }
        }
        
        let dissMissMain:(()->Void) = {
            DebugMainWindow.share.showOrDissMiss()
        }
        block(callBackCon,dissMissMain)
    }
    
    private func addNormalItem(itemImage:UIImage, itemTitle:String, itemClickBlock:@escaping((_ callBack: ((UIViewController,Int,Bool)->Void),_ dissMissMain:(()->Void))->Void))->Void{
        
        var dic:Dictionary<String,Any> = [:]
        dic.updateValue(itemImage, forKey: itemImageStr)
        dic.updateValue(itemTitle, forKey: itemTitleStr)
        dic.updateValue(itemClickBlock, forKey: itemClickBlockStr)
        self.itemArray.append(dic)
    }
}
